package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.CourseLikeEntity;
import com.hy.admin.entity.FeedbackEntity;
import com.hy.admin.service.CourseLikeService;
import com.hy.admin.service.FeedbackService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yangqq on 18/4/3.
 * 点赞
 */
@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    protected final Logger logBase = LoggerFactory.getLogger(FeedbackController.class);
    @Autowired
    private FeedbackService feedbackService;

    /**
     * 添加评论
     * @param apiRequestDTO
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponseDTO add(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("add_feedback_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        FeedbackEntity feedbackEntity= gson.fromJson(apiRequestDTO.getRequestData(), FeedbackEntity.class);
        //获取请求数据
        ServicesResult<Boolean> commentResponse = feedbackService.add(feedbackEntity);
        apiResponseDTO.initServicesResult(commentResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(commentResponse.getResult()));
        logBase.info("add_feedback_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
    
}
